ext.define('extension.utils', function() {

var rxswf = /\.swf\b/i;
var rxsrc = /src|movie/i;
var rxclsid = /D27CDB6E-?AE6D-?11cf-?96B8-?444553540000/i;
var rxtype = /application\/(x-shockwave-flash|futuresplash)/i;

function isNestedObjectElement(element) {
    var parent = element.parentNode;
    return !(parent instanceof HTMLObjectElement && isObjectElement(parent));
}

function isObjectElement(element) {
    if (!isNestedObjectElement(element)) return false;
    if (rxclsid.test(element.getAttribute('classid'))) return false;
    return rxtype.test(element.type) || rxswf.test(element.data) || isObjectParams(element);
}

function isEmbedElement(element) {
    if (!isNestedObjectElement(element)) return false;
    return rxtype.test(element.type) || rxswf.test(element.src);
}

function isObjectParams(element) {
    if (element.firstChild) {
        var rxs = rxswf, rxt = rxtype;
        for (var child = element.lastElementChild; child; child = child.previousElementSibling) {
            if (child instanceof HTMLParamElement) {
                var name = child.name;
                if ((name == 'type' && rxt.test(child.value)) ||
                    ((name == 'movie' || name == 'src') && rxs.test(child.value)) ||
                    (name == 'flashvars')) {
                    return true;
                }
            }
        }
    }
    return false;
}

function getParamElement(element, name) {
    var param = element.querySelectorAll('param[name=' + name + ']');
    return param[param.length - 1];
}

function copyBoundingBox(target, source) {
    var view = window.getComputedStyle(target, null);
    source.style.width = view.width;
    source.style.height = view.height;
}

function createPlaceholderToolbar(element, config) {
    var div = document.createElement('div');
    div.style.cssText = config.toolbar;
    return element.appendChild(div);
}

function createElementPlaceholder(element, config) {
    var view = window.getComputedStyle(element, null);
    var style = {
        'width': view.width,
        'height': view.height,
        'top': view.top,
        'left': view.left
    };
    if (view.position !== 'static')
        style.position = view.position;
    var cssText = '';
    for (var k in style)
        cssText += k + ':' + style[k] + ';';
    var div = document.createElement('div');
    div.style.cssText = cssText + config.panel;
    if (!config.panelclick)
        div.style.cursor = 'default';
    return div;
}

function getPluginSRC(element) {
    var src = '';
    if (element instanceof HTMLEmbedElement)
        src = element.src;
    else if (element instanceof HTMLObjectElement) {
        if (element.data)
            src = element.data;
        else {
            for (var child = element.lastElementChild; child; child = child.previousElementSibling) {
                if (child instanceof HTMLParamElement)
                    if (src = getPluginSRC(child)) break;
            }
        }
    }
    else if (element instanceof HTMLParamElement) {
        if (rxsrc.test(element.name))
            src = element.value;
    }
    return src.trim();
}

function getPluginParam(element, name) {
    var value = '', temp;
    if (element instanceof HTMLObjectElement) {
        for (var child = element.lastElementChild; child; child = child.previousElementSibling) {
            if (child instanceof HTMLParamElement) {
                if (temp = getPluginParam(child, name)) {
                    value = temp;
                    break;
                }
            }
        }
    }
    else if (element instanceof HTMLEmbedElement)
        element.hasAttribute(name) && (value = element.getAttribute(name));
    else if (element instanceof HTMLParamElement)
        value = name.toLowerCase() == element.name.toLowerCase() ? element.value : '';
    return value.trim();
}

function isElement(element) {
    return element instanceof HTMLElement;
}

function isValidPlugin(element) {
    if (element instanceof HTMLObjectElement)
        return isObjectElement(element);
    if (element instanceof HTMLEmbedElement)
        return isEmbedElement(element);
    return false;
}

function removePluginParam(element, name) {
    if (element instanceof HTMLObjectElement) {
        var param = getParamElement(element, name);
        if (param)
            element.removeChild(param);
    }
    else if (element instanceof HTMLEmbedElement)
        element.removeAttribute(name);
}

function setPluginParam(element, test, name, value) {
    if (element instanceof HTMLObjectElement) {
        var param = getParamElement(element, name);
        if (param ? !extension.utils.regexp(test).test(param.value) : true) {
            param = document.createElement('param');
            param.name = name;
            param.value = value;
            element.appendChild(param);
            return true;
        }
    }
    else if (element instanceof HTMLEmbedElement) {
        if (!extension.utils.regexp(test).test(element.getAttribute(name))) {
            element.setAttribute(name, value);
            return true;
        }
    }
    return false;
}

return {
    copyBoundingBox: copyBoundingBox,
    createPlaceholderToolbar: createPlaceholderToolbar,
    createElementPlaceholder: createElementPlaceholder,
    getPluginSRC: getPluginSRC,
    getPluginParam: getPluginParam,
    isElement: isElement,
    isValidPlugin: isValidPlugin,
    removePluginParam: removePluginParam,
    setPluginParam: setPluginParam
};

});
